<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\form\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use plugins\form\admin\model\Set;
use plugins\form\admin\model\Message as MessageModel;
/**
 * 表单留言列表
 */
class Message extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
    	if ($this->request->isPost()) {
            $input = input('post.');
            $search = ['keyword', 'date', 'id'];
            $order = [$input['prop'] => $input['order']];
            $count = MessageModel::withSearch($search, $input)->count();
            $data  = MessageModel::withSearch($search, $input)->with("user")->order($order)->page($input['page'], $input['pageSize'])->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
    	} else {
            $form = Set::field('id,name,field')->where('status', 1)->select();
            View::assign('form', $form);
    		return View::fetch();
    	}
    }
    
    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            MessageModel::where('id', input('post.id'))->update(['read' => 1]);
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            MessageModel::destroy(input('post.ids'));
            return json(['status' => 'success', 'message' => '删除成功']);
        }
    }
}